#include <iostream>
using namespace std;


class fraction
{
    int above;
    int below;

    void reduction();
    void makeCommond(fraction);

public:
    fraction(int a=0,int b=1){
        above=a;
        below=b;
    };
    fraction(fraction const &f) ;
    fraction add(const fraction &f) const;
    fraction sub(const fraction &f) const;
    fraction mul(const fraction &f) const;
    fraction div(const fraction &f) const;
    fraction reciprocal() const;
    bool equal(const fraction &f) const;
    bool greaterThan(const fraction &f) const;
    bool lessThan(const fraction &f) const;
    void display();
    void input();
    friend fraction adds(fraction const &f1, fraction const &f2);
};

/**
 * @brief Construct a new fraction::fraction object 
 * 
 * @param f 
 */
fraction::fraction(fraction const &f) : above(f.above), below(f.below) {}

/**
 * @brief 加法函数
 * 
 * @param f 另一个加数
 * @return fraction 结果
 */
fraction fraction::add(const fraction &f) const
{   
    fraction result;
    result.above=above*f.below+below*f.above;
    result.below=below*f.below;
    result.reduction();

    return result;
}

/**
 * @brief 减法函数
 * eg:f1-f2,f1.sub(f2)
 * @param f 减数
 * @return fraction 返回结果
 */
fraction fraction::sub(const fraction &f) const
{
    fraction result;
    result.above = above * f.below - f.above * below;
    result.below = below * f.below;
    result.reduction();
    return result;
}
/**
 * @brief 乘法函数
 * 
 * @param f 
 * @return fraction 
 */
fraction fraction::mul(const fraction &f) const
{
    fraction result;
    result.above = above * f.above;
    result.below = below * f.below;
    result.reduction();
    return result;
}

/**
 * @brief 除法函数
 * 
 * @param f 
 * @return fraction 
 */
fraction fraction::div(const fraction &f) const 
{
    fraction result;
    result.above = above * f.below;
    result.below = below * f.above;
    result.reduction();
    return result;
}

/**
 * @brief 倒数函数
 * 
 * @return fraction 
 */
fraction fraction::reciprocal() const
{
    fraction result;
    result.above = below;
    result.below = above;
    return result;
}

bool fraction::equal(const fraction &f) const
{
    return (above * f.below == below * f.above);
}

bool fraction::greaterThan(const fraction &f) const
{
    return (above * f.below > below * f.above);
}

bool fraction::lessThan(const fraction &f) const
{
    return (above * f.below < below * f.above);
}

void fraction::display()
{
    cout << above << "/" << below<<endl;
}

void fraction::input()
{
    cout << "Enter the numerator: ";
    cin >> above;
    cout << "Enter the denominator: ";
    cin >> below;
}

void fraction::reduction()
{
    int gcd = 1;
    int smaller = (above < below) ? above : below;
    for (int i = 2; i <= smaller; i++)
    {
        if (above % i == 0 && below % i == 0)
            gcd = i;
    }
    above /= gcd;
    below /= gcd;
}

fraction adds(fraction const &f1, fraction const &f2)
{
    fraction result;
    result.above = f1.above * f2.below + f2.above * f1.below;
    result.below = f1.below * f2.below;
    result.reduction();
    return result;
}

int main(int argc,char **argv)
{
    fraction f1(3, 4);
    fraction f2(1, 2);
    fraction f3;

    //分数运用
    cout<<"f1: ";
    f1.display();
    cout<<"f2: ";
    f2.display();
    cout<<"f3: ";
    f3.display();

    //加减乘除运算
    cout<<"f1+f2: ";
    f3=f1.add(f2);
    f3.display();
    cout<<"f1-f2: ";
    f3=f1.sub(f2);
    f3.display();
    cout<<"f1*f2: ";
    f3=f1.mul(f2);
    f3.display();
    cout<<"f1/f2: ";
    f3=f1.div(f2);
    f3.display();

    //逻辑运算
    fraction f4(3,4);
    cout<<"f1==f4: "<<f1.equal(f4)<<endl;
    cout<<"f1>f2: "<<f1.greaterThan(f2)<<endl;
    cout<<"f1<f2: "<<f1.lessThan(f2)<<endl;

    //输入测试
    cout<<"change f1: "<<endl;
    f1.input();
    cout<<"result:";
    f1.display();

    //倒数
    cout<<"f2.reciprocal:";
    fraction f5=f2.reciprocal();
    f5.display();
    
    //友元函数测试
    fraction f6=adds(f1, f2);
    cout<<"f1+f2: ";
    f6.display();

    return 0;
}